package com.jp.knowledge.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.activity.TopicActivity;
import com.jp.knowledge.adapter.TopicOtherAdapter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.OtherTopic;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiayen on 16/10/12.
 * <p>
 * 话题 (资讯公司话题)
 */

public class TopicOtherFragment extends BaseFragment implements ResultSubscriber.OnResultListener, CanRefreshLayout.OnLoadMoreListener, CanRefreshLayout.OnRefreshListener {

    private View contentView;

    @ViewInject(R.id.can_content_view)
    private ListView listView;

    private TopicOtherAdapter adapter;
    @ViewInject(R.id.m_refresh)
    private CanRefreshLayout  mRefresh;



    private List<OtherTopic> list;


    public boolean refresh;//是否刷新

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.framgent_topic_other, null);
        this.mContext = getActivity();
        x.view().inject(this,contentView);
        initView();
        return contentView;
    }

    private void initView() {
        if (list == null) list = new ArrayList<>();
        adapter = new TopicOtherAdapter(mContext, list);
        listView.setAdapter(adapter);

        getOtherTopicList();
//        mRefresh.setLoadMoreEnabled(true);
//        mRefresh.setOnLoadMoreListener(this);
//        mRefresh.setOnRefreshListener(this);

    }

    private void getOtherTopicList() {
        String infoId = ((TopicActivity)mContext).getInfoId();
        JsonObject param = new JsonObject();
        param.addProperty("infoId",infoId);
        HttpHelper.getInstance(mContext).topicOtherList(param,1,this);
    }

    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {

    }

    @Override
    public void onError(int requestType) {

    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code = data.getErrcode();
        if(code==0){
            ArrayList<LinkedTreeMap<String,Object>> maps = (ArrayList<LinkedTreeMap<String,Object>>) data.getData();
            list = JsonUtil.getInstance().listMapToObject(maps,OtherTopic.class);
            adapter.updataAdapter(list);
        }else if(code==110113){//未登录

        }
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onRefresh() {

    }
}
